#include "StdAfx.h"
#include "TerminalSettings.h"
#include "AsciiConstants.h"

CTerminalSettings CTerminalSettings::Default;

CTerminalSettings::CTerminalSettings(void)
	: name(_T("Default"))
{
	unsigned char up[] = { AsciiByte::ESC, AsciiByte::LBR, AsciiByte::A };
	unsigned char down[] = { AsciiByte::ESC, AsciiByte::LBR, AsciiByte::B };

	this->keyToSequenceMapping[VK_UP].push_back(AsciiByte::ESC);
	this->keyToSequenceMapping[VK_UP].push_back(AsciiByte::LBR);
	this->keyToSequenceMapping[VK_UP].push_back(AsciiByte::A);

	this->keyToSequenceMapping[VK_DOWN].push_back(AsciiByte::ESC);
	this->keyToSequenceMapping[VK_DOWN].push_back(AsciiByte::LBR);
	this->keyToSequenceMapping[VK_DOWN].push_back(AsciiByte::B);

	this->keyToSequenceMapping[VK_RIGHT].push_back(AsciiByte::ESC);
	this->keyToSequenceMapping[VK_RIGHT].push_back(AsciiByte::LBR);
	this->keyToSequenceMapping[VK_RIGHT].push_back(AsciiByte::C);

	this->keyToSequenceMapping[VK_LEFT].push_back(AsciiByte::ESC);
	this->keyToSequenceMapping[VK_LEFT].push_back(AsciiByte::LBR);
	this->keyToSequenceMapping[VK_LEFT].push_back(AsciiByte::D);

	this->keyToSequenceMapping[VK_INSERT].push_back(AsciiByte::ESC);
	this->keyToSequenceMapping[VK_INSERT].push_back(AsciiByte::LBR);
	this->keyToSequenceMapping[VK_INSERT].push_back(AsciiByte::D2);
	this->keyToSequenceMapping[VK_INSERT].push_back(AsciiByte::Tilde);

	this->keyToSequenceMapping[VK_DELETE].push_back(AsciiByte::ESC);
	this->keyToSequenceMapping[VK_DELETE].push_back(AsciiByte::LBR);
	this->keyToSequenceMapping[VK_DELETE].push_back(AsciiByte::D3);
	this->keyToSequenceMapping[VK_DELETE].push_back(AsciiByte::Tilde);

	this->keyToSequenceMapping[VK_HOME].push_back(AsciiByte::ESC);
	this->keyToSequenceMapping[VK_HOME].push_back(AsciiByte::LBR);
	this->keyToSequenceMapping[VK_HOME].push_back(AsciiByte::D1);
	this->keyToSequenceMapping[VK_HOME].push_back(AsciiByte::Tilde);

	this->keyToSequenceMapping[VK_END].push_back(AsciiByte::ESC);
	this->keyToSequenceMapping[VK_END].push_back(AsciiByte::LBR);
	this->keyToSequenceMapping[VK_END].push_back(AsciiByte::D4);
	this->keyToSequenceMapping[VK_END].push_back(AsciiByte::Tilde);

	this->keyToSequenceMapping[VK_PRIOR].push_back(AsciiByte::ESC);
	this->keyToSequenceMapping[VK_PRIOR].push_back(AsciiByte::LBR);
	this->keyToSequenceMapping[VK_PRIOR].push_back(AsciiByte::D5);
	this->keyToSequenceMapping[VK_PRIOR].push_back(AsciiByte::Tilde);

	this->keyToSequenceMapping[VK_NEXT].push_back(AsciiByte::ESC);
	this->keyToSequenceMapping[VK_NEXT].push_back(AsciiByte::LBR);
	this->keyToSequenceMapping[VK_NEXT].push_back(AsciiByte::D6);
	this->keyToSequenceMapping[VK_NEXT].push_back(AsciiByte::Tilde);
}

CTerminalSettings::~CTerminalSettings(void)
{
}

bool CTerminalSettings::MapKeyToSequence(UINT key, unsigned char* &buffer, int &count)
{
	map<UINT, vector<unsigned char>>::iterator mapIterator = this->keyToSequenceMapping.find(key);
	if (mapIterator == this->keyToSequenceMapping.end())
	{
		return false;
	}
	else
	{
		vector<unsigned char> sequence = mapIterator->second;
		count = sequence.size();
		buffer = new unsigned char[count];
		for (int i = 0; i < count; i ++)
			buffer[i] = sequence[i];
		return true;
	}
}